import marimo

__generated_with = "0.13.7"
app = marimo.App(width="medium")


@app.cell
def _():
    import marimo as mo
    return (mo,)


@app.cell
def _(mo):
    mo.md(r"""# 线性代数""")
    return


@app.cell
def _(mo):
    mo.md(r"""## 标量""")
    return


@app.cell
def _():
    import torch

    x = torch.tensor(3.0)
    y = torch.tensor(2.0)

    x + y, x * y, x / y, x**y
    return (torch,)


@app.cell
def _(mo):
    mo.md(r"""## 向量""")
    return


@app.cell
def _(torch):
    x1 = torch.arange(4, dtype=torch.float32)
    x1, x1[3]
    return (x1,)


@app.cell
def _(mo):
    mo.md(r"""### 长度、维度和形状""")
    return


@app.cell
def _(x1):
    len(x1),x1.shape
    return


@app.cell
def _(mo):
    mo.md(r"""## 矩阵""")
    return


@app.cell
def _(torch):
    A = torch.arange(20).reshape(5, 4)
    print(A)
    print(A.T)
    return


@app.cell
def _(torch):
    B = torch.tensor([[1, 2, 3], [2, 0, 4], [3, 4, 5]])
    B == B.T
    return


@app.cell
def _(mo):
    mo.md(r"""## 张量""")
    return


@app.cell
def _(torch):
    X1 = torch.arange(24).reshape(2, 3, 4)
    X1
    return


@app.cell
def _(mo):
    mo.md(r"""## 张量算法的基本性质""")
    return


@app.cell
def _(torch):
    A1 = torch.arange(20, dtype=torch.float32).reshape(5, 4)
    B1 = A1.clone()  # 通过分配新内存，将A的一个副本分配给B
    A1, A1 + B1, A1 * B1
    return (A1,)


@app.cell
def _(A1):
    a1 = 2
    a1 * A1, (a1 + A1).shape
    return


@app.cell
def _(mo):
    mo.md(r"""### 降维""")
    return


@app.cell
def _(A1):
    A1.shape,A1.sum()
    return


@app.cell
def _(A1):
    print(A1)
    A1_sum_axis0 = A1.sum(axis=0)
    A1_sum_axis0, A1_sum_axis0.shape,A1.sum(axis=1)
    return


@app.cell
def _(A1):
    A1.sum(axis=[0, 1])
    return


@app.cell
def _(mo):
    mo.md(r"""#### 非降维求和""")
    return


@app.cell
def _(A1):
    sum_A1 = A1.sum(axis=1, keepdims=True)
    sum_A1
    return (sum_A1,)


@app.cell
def _(A1, sum_A1):
    A1/sum_A1
    return


@app.cell
def _(A1):
    A1/A1.sum(axis=0)
    return


@app.cell
def _(A1):
    print(A1)
    A1.cumsum(axis=1), A1.cumsum(axis=0)
    return


@app.cell
def _(mo):
    mo.md(r"""## 点积""")
    return


@app.cell
def _(torch, x1):
    y1 = torch.ones(4, dtype = torch.float32)
    x1, y1, torch.dot(x1, y1)  ,torch.sum(x1*y1)
    return


@app.cell
def _(mo):
    mo.md(r"""## 矩阵-向量积""")
    return


@app.cell
def _(A1, torch, x1):
    A1.shape, x1.shape, torch.mv(A1, x1)
    return


@app.cell
def _(mo):
    mo.md(r"""## 矩阵-矩阵乘法""")
    return


@app.cell
def _(A1, torch):
    B2 = torch.ones(4, 3)
    torch.mm(A1, B2)
    return


@app.cell
def _(mo):
    mo.md(r"""## 范数""")
    return


@app.cell
def _(torch):
    u = torch.tensor([3.0, -4.0])
    torch.norm(u)
    return (u,)


@app.cell
def _(torch, u):
    torch.abs(u).sum()
    return


@app.cell
def _(torch):
    torch.norm(torch.ones((4, 9)))
    return


@app.cell
def _(mo):
    mo.md(r"""#### 范数和目标""")
    return


@app.cell
def _(mo):
    mo.md(r"""## 关于线性代数的信息""")
    return


@app.cell
def _(mo):
    mo.md(r"""## 练习""")
    return


@app.cell
def _(mo):
    mo.md(r""" """)
    return


if __name__ == "__main__":
    app.run()
